#encoding:UTF-8
import re
import mylib
import pythonlibSB
from sets import Set

def get_hot_link():
    
    try:
        
        results = []
        
        content = mylib.get_content('http://kienthuc.net.vn/')
        
        hot_10_link = []
        hot_10_content = pythonlibSB.s_string(content,'class="box-bd hotnews"','td')
        reg = re.compile('(<a)(.*?)(href=")(.*?)(")')
        if reg.search(hot_10_content):
            url10 = reg.search(hot_10_content).group(4)
            url10save = url10 if url10.find('http://')!=-1 else ('http://kienthuc.net.vn'+url10)
            hot_10_link.append(url10save)
        results.append(hot_10_link)
        
        hot_9_link = []
        hot_9_content = pythonlibSB.s_string(content,'class="newstop row1"','div')
        for i in re.finditer('(<a)(.*?)(href=")(.*?)(")(.*?)(class="avatar")',hot_9_content):
            url9 = i.group(4) if i.group(4).find('http://')!=-1 \
                    else ('http://kienthuc.net.vn'+i.group(4))
            hot_9_link.append(url9)
        results.append(hot_9_link)
        
        hot_8_link = []
        hot_8_content = pythonlibSB.s_string(content,'class="newsfocus pad10 row1"','div')
        for j in re.finditer('(<a)(.*?)(href=")(.*?)(")',hot_8_content):
            url8 = j.group(4) if j.group(4).find('http://')!=-1 \
                    else ('http://kienthuc.net.vn'+j.group(4))
            hot_8_link.append(url8)
        results.append(hot_8_link)
        
        hot_7_link = []
        mycontent = pythonlibSB.s_string(content,'class="col1"','div')
        pos1 = mycontent.find('class="list-semi"')
        while pos1 != -1:
            pos2 = mycontent.find('class="title-1"',pos1)
            if pos2 == -1:
                break
            t = mycontent[pos1:pos2]
            reg2 = re.compile('(<a)(.*?)(href=")(.*?)(")')
            if reg2.search(t):
                url7 = reg2.search(t).group(4) if reg2.search(t).group(4).find('http://')!=-1\
                    else ('http://kienthuc.net.vn'+reg2.search(t).group(4))
                hot_7_link.append(url7)
                
            pos1 = pos2 + 1
        
        for l in hot_7_link:
            if l in (hot_10_link,hot_9_link,hot_8_link):
                hot_7_link.remove(l)

        results.append(hot_7_link)
        
        return results
    except:
        return []
    
if __name__ == "__main__":
    results = get_hot_link()
    print results
    hot7 = results[3]
    print hot7
    print len(hot7)
    